import {
    getWXQRCode,
    getIsSuccess
} from '../../../api/index.js'
import {getOrderAlllist} from "../../../api/index"

export default {
    props: {
        orderNumber: String,
        handleStep:Function
    },
    data() {
        return {
            countNum:{},
            tableData: [],
            orderInfo:{},
            payment: 2,
            codeUrl: '',
            zfbCode: require('../../../assets/code.jpg'),
            setIntervalID:null,
        }
    },
    methods: {
        handleWXCode() {
            const loading = this.$loading({
                lock: true,
                text: '正在获取...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            })

            getWXQRCode(this.orderNumber).then(res => {
                this.codeUrl = res.data
                loading.close()
                this.handleIsSuccess()
            }).catch(() => {
                loading.close()
                // this.$message.error("获取支付二维码失败")
            })
        },
        handlePaymentMethod(type = 2){
            // 1 支付宝，2 微信
            if(type == 2){
                this.handleWXCode()
                this.payment = 2
            }else{
                this.payment = 1
            }
        },
        //获取订单信息
        getOrderInfo() {
            getOrderAlllist({
                orderNo: this.$route.query.orderNumber,
                page:1,
                pageSize:100
            }).then(res => {
                if (res.data) {
                    let result = []
                    res.data.result.list.forEach(item=>{
                        item.titleList.forEach(list=>{
                            list.children.forEach(child=>{
                                result.push({
                                    projectName:child.prodectName,
                                    orderDetilesId:this.$route.query.orderNumber,
                                    realProjectPrice:child.projecetPrice
                                })
                            })
                        })
                    })

                    let sumPrice = res.data.result.list[0].totalPrice

                    this.countNum = {
                        orderCount:result.length,
                        sumPrice:sumPrice
                    }

                    if(result.length>1){
                        this.tableData = result
                    }else{
                        this.orderInfo = result[0]
                    }
                }
            }).catch(() => {
                // this.$message.error("获取订单列表失败")
            })
        },

        // 获取是否支付成功
        handleIsSuccess(){
            this.setIntervalID = setInterval(()=>{
                getIsSuccess({
                    orderNo:this.$route.query.orderNumber
                }).then(res=>{
                    if (res.data.code === 200){
                        clearInterval(this.setIntervalID)
                        this.handleStep()
                    }
                }).catch(err=>{
                    console.log('支付失败')
                })
            },2000)

        }

    },
    created() {
        this.handlePaymentMethod()
        this.getOrderInfo()
    },
    destroyed(){
        clearInterval(this.setIntervalID)
    },
}
